package com.simen.album;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class LauncherActivity extends AppCompatActivity {

    public final static int FROM_ALBUM = 1;
    public final static String TAG = LauncherActivity.class.getCanonicalName();
    private AlbumView album;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);

        album = AlbumView.attachWindow(LauncherActivity.this, Color.BLACK);
        album.setVisibility(View.GONE);
        album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                album.cancelView(new AlbumView.OnAnimatorEndListener() {
                    @Override
                    public void scaleAnimationEnd(AlbumView albumView) {
                        albumView.setVisibility(View.GONE);
                    }
                });
            }
        });
        findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(intent, FROM_ALBUM);
                } else {
                    Toast.makeText(LauncherActivity.this, "找不到相册程序", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case FROM_ALBUM:
                    if (data == null) {
                        return;
                    }
                    Uri uri = data.getData();
                    Log.d(TAG, "onActivityResult: " + uri.getPath() + " " + getImagePathFromUri(uri));
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(getImagePathFromUri(uri), options);
                    album.setVisibility(View.VISIBLE);
                    album.setImage(getImagePathFromUri(uri), new Rect(0, 0, options.outWidth, options.outHeight));
                    break;

            }
        }
    }

    public String getImagePathFromUri(Uri uri) {
        String res = null;
        String[] proj = {MediaStore.Images.Media.DATA};
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
            if (cursor.moveToFirst()) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                res = cursor.getString(column_index);
            }
            cursor.close();
        } else {
            Cursor cursor = managedQuery(uri, proj, null, null, null);
            int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                res = cursor.getString(actual_image_column_index);
            }
            cursor.close();
        }
        return res;
    }
}
